<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="Shiro步骤或注意事项：  Jar包     &amp;lt;dependency&amp;gt;     &amp;lt;groupId&amp;gt;org.apache.shiro&amp;lt;/groupId&amp;gt;     &amp;lt;artifactId&amp;gt;shiro-core&amp;lt;/artifactId&amp;gt;     &amp;lt;version&amp;gt;1.2.2&amp;lt;/version&amp;gt; &amp;lt;/depende">
<meta property="og:type" content="article">
<meta property="og:title" content="shiro">
<meta property="og:url" content="http://smallkk.gitee.io/2018/01/12/Shiro/index.html">
<meta property="og:site_name" content="Hi , 小坤坤">
<meta property="og:description" content="Shiro步骤或注意事项：  Jar包     &amp;lt;dependency&amp;gt;     &amp;lt;groupId&amp;gt;org.apache.shiro&amp;lt;/groupId&amp;gt;     &amp;lt;artifactId&amp;gt;shiro-core&amp;lt;/artifactId&amp;gt;     &amp;lt;version&amp;gt;1.2.2&amp;lt;/version&amp;gt; &amp;lt;/depende">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-04-30T19:10:39.381Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="shiro">
<meta name="twitter:description" content="Shiro步骤或注意事项：  Jar包     &amp;lt;dependency&amp;gt;     &amp;lt;groupId&amp;gt;org.apache.shiro&amp;lt;/groupId&amp;gt;     &amp;lt;artifactId&amp;gt;shiro-core&amp;lt;/artifactId&amp;gt;     &amp;lt;version&amp;gt;1.2.2&amp;lt;/version&amp;gt; &amp;lt;/depende">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://smallkk.gitee.io/2018/01/12/Shiro/"/>





  <title>shiro | Hi , 小坤坤</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Hi , 小坤坤</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallkk.gitee.io/2018/01/12/Shiro/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="S-Name Small·kk">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hi , 小坤坤">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">shiro</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-01-12T09:30:52+08:00">
                2018-01-12
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="Shiro步骤或注意事项："><a href="#Shiro步骤或注意事项：" class="headerlink" title="Shiro步骤或注意事项："></a>Shiro步骤或注意事项：</h2><hr>
<ol start="0">
<li><p>Jar包</p>
   <!--apache shiro  -->
<pre><code>&lt;dependency&gt;
    &lt;groupId&gt;org.apache.shiro&lt;/groupId&gt;
    &lt;artifactId&gt;shiro-core&lt;/artifactId&gt;
    &lt;version&gt;1.2.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.apache.shiro&lt;/groupId&gt;
    &lt;artifactId&gt;shiro-web&lt;/artifactId&gt;
    &lt;version&gt;1.2.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.apache.shiro&lt;/groupId&gt;
    &lt;artifactId&gt;shiro-spring&lt;/artifactId&gt;
    &lt;version&gt;1.2.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;!--apache shiro  end--&gt; 
</code></pre></li>
</ol>
<ol>
<li>配置spring-shiro.xml 引入到 spring-cfg.xml 或者直接配置到spring-cfg.xml里面</li>
</ol>
<p>注意拦截放行的路径！！！！！</p>
<pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;beans xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
       xmlns=&quot;http://www.springframework.org/schema/beans&quot;
       xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd&quot;&gt;
    &lt;!-- 继承自AuthorizingRealm的自定义Realm,即指定Shiro验证用户登录 --&gt;
    &lt;bean id=&quot;myRealm&quot; class=&quot;com.smallkk.common.security.MyRealm&quot;&gt;
        &lt;property name=&quot;credentialsMatcher&quot; ref=&quot;passwordMatcher&quot;/&gt;
    &lt;/bean&gt;
    &lt;bean id=&quot;passwordMatcher&quot; class=&quot;com.smallkk.common.security.CustomCredentialsMatcher&quot;/&gt;
    &lt;!-- Shiro默认会使用Servlet容器的Session,可通过sessionMode属性来指定使用Shiro原生Session --&gt;
    &lt;!-- 即&lt;property name=&quot;sessionMode&quot; value=&quot;native&quot;/&gt;,详细说明见官方文档 --&gt;
    &lt;!-- 这里主要是设置自定义的单Realm应用,若有多个Realm,可使用&apos;realms&apos;属性代替 --&gt;
    &lt;bean id=&quot;securityManager&quot; class=&quot;org.apache.shiro.web.mgt.DefaultWebSecurityManager&quot;&gt;
        &lt;property name=&quot;realm&quot; ref=&quot;myRealm&quot;/&gt;
    &lt;/bean&gt;
    &lt;!-- Shiro主过滤器本身功能十分强大,其强大之处就在于它支持任何基于URL路径表达式的、自定义的过滤器的执行 --&gt;
    &lt;!-- Web应用中,Shiro可控制的Web请求必须经过Shiro主过滤器的拦截,Shiro对基于Spring的Web应用提供了完美的支持 --&gt;
    &lt;bean id=&quot;shiroFilter&quot; class=&quot;org.apache.shiro.spring.web.ShiroFilterFactoryBean&quot;&gt;
        &lt;!-- Shiro的核心安全接口,这个属性是必须的 --&gt;
        &lt;property name=&quot;securityManager&quot; ref=&quot;securityManager&quot;/&gt;
        &lt;!-- 要求登录时的链接(可根据项目的URL进行替换),非必须的属性,默认会自动寻找Web工程根目录下的&quot;/login.jsp&quot;页面 --&gt;
        &lt;property name=&quot;loginUrl&quot; value=&quot;/user/login&quot;/&gt;
        &lt;!-- 登录成功后要跳转的连接(本例中此属性用不到,因为登录成功后的处理逻辑在LoginController里硬编码为main.jsp了) --&gt;
        &lt;!-- &lt;property name=&quot;successUrl&quot; value=&quot;/system/main&quot;/&gt; --&gt;
        &lt;!-- 用户访问未对其授权的资源时,所显示的连接 --&gt;
        &lt;!-- 若想更明显的测试此属性可以修改它的值,如unauthor.jsp,然后用[玄玉]登录后访问/admin/listUser.jsp就看见浏览器会显示unauthor.jsp --&gt;
        &lt;!--&lt;property name=&quot;unauthorizedUrl&quot; value=&quot;/error/403.html&quot;/&gt;--&gt;
        &lt;!-- Shiro连接约束配置,即过滤链的定义 --&gt;
        &lt;!-- 此处可配合我的这篇文章来理解各个过滤连的作用http://blog.csdn.net/jadyer/article/details/12172839 --&gt;
        &lt;!-- 下面value值的第一个&apos;/&apos;代表的路径是相对于HttpServletRequest.getContextPath()的值来的 --&gt;
        &lt;!-- anon：它对应的过滤器里面是空的,什么都没做,这里.do和.jsp后面的*表示参数,比方说login.jsp?main这种 --&gt;
        &lt;!-- authc：该过滤器下的页面必须验证后才能访问,它是Shiro内置的一个拦截器org.apache.shiro.web.filter.authc.FormAuthenticationFilter --&gt;
        &lt;property name=&quot;filterChainDefinitions&quot;&gt;
            &lt;value&gt;
                /static/**=anon
                /login=anon
                &lt;!--/user/userinfo=authc--&gt;
                /user/**=anon
                /jump/**=authc
            &lt;/value&gt;
        &lt;/property&gt;
    &lt;/bean&gt;
    &lt;!--&lt;bean id=&quot;sessionManager&quot; class=&quot;org.apache.shiro.web.session.mgt.DefaultWebSessionManager&quot;&gt;--&gt;
    &lt;!--&amp;lt;!&amp;ndash; 超时时间 &amp;ndash;&amp;gt;--&gt;
    &lt;!--&lt;property name=&quot;globalSessionTimeout&quot; value=&quot;1&quot;/&gt;--&gt;
    &lt;!--&lt;property name=&quot;sessionDAO&quot; ref=&quot;sessionDAO&quot;/&gt;--&gt;
    &lt;!--&amp;lt;!&amp;ndash; 定时检查失效的session &amp;ndash;&amp;gt;--&gt;
    &lt;!--&lt;property name=&quot;sessionValidationSchedulerEnabled&quot; value=&quot;true&quot;/&gt;--&gt;
    &lt;!--&lt;/bean&gt;--&gt;
    &lt;bean id=&quot;sessionDAO&quot;
          class=&quot;org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO&quot;&gt;
        &lt;property name=&quot;activeSessionsCacheName&quot; value=&quot;shiro-activeSessionCache&quot;/&gt;
    &lt;/bean&gt;
    &lt;!-- 保证实现了Shiro内部lifecycle函数的bean执行 --&gt;
    &lt;bean id=&quot;lifecycleBeanPostProcessor&quot; class=&quot;org.apache.shiro.spring.LifecycleBeanPostProcessor&quot;/&gt;
&lt;/beans&gt;
</code></pre><ol start="2">
<li><p>在spring-mvc.xml开启基础MVC层基于Controller的“权限启用”注解</p>
<pre><code>&lt;aop:aspectj-autoproxy proxy-target-class=&quot;true&quot;/&gt;

&lt;bean class=&quot;org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor&quot;&gt;
    &lt;property name=&quot;securityManager&quot; ref=&quot;securityManager&quot;/&gt;
&lt;/bean&gt;
</code></pre></li>
<li><p>在web.xml里面设置权限的过滤器</p>
<pre><code>&lt;filter&gt;
        &lt;filter-name&gt;shiroFilter&lt;/filter-name&gt;
        &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
        &lt;async-supported&gt;true&lt;/async-supported&gt;
        &lt;init-param&gt;
            &lt;!-- 该值缺省为false,表示生命周期由SpringpplicationContext管理,设置为true则表示
            由ServletContainer管理 --&gt;
          &lt;param-name&gt;targetFilterLifecycle&lt;/param-name&gt;
            &lt;param-value&gt;true&lt;/param-value&gt;
        &lt;/init-param&gt;
    &lt;/filter&gt;
    &lt;filter-mapping&gt;
        &lt;filter-name&gt;shiroFilter&lt;/filter-name&gt;
        &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
    &lt;/filter-mapping&gt;
</code></pre></li>
<li><p>可能有5张表①用户表②角色表③权限表④用户-角色表⑤角色权限表</p>
</li>
<li><p>在用户实体中引入角色列表，在角色中引入权限列表；三表关联，查出一个用户的同时可以附带他的角色及权限(Mapper.xml里面同步三张表)</p>
</li>
<li><p>Shiro三种管理方式： ①纯数据库管理，对权限增删查改 ②数据库配合权限内容路径注解 ③数据库配合角色注解(注意shiro.xml里面的anon这样的拦截配置)</p>
</li>
</ol>
<h4 id="7"><a href="#7" class="headerlink" title="==7==."></a>==7==.</h4><p>①：MyRealm :   extends AuthorizingRealm</p>
<pre><code>① protected AuthorizationInfo doGetAuthorizationInfo
(PrincipalCollection principalCollection) {
//授权方法
② protected AuthenticationInfo doGetAuthenticationInfo
(AuthenticationToken authenticationToken) throws AuthenticationException {
//认证登录方法
</code></pre><p>②：CustomCredentialsMatcher :  extends SimpleCredentialsMatcher {</p>
<pre><code>public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
//自定义密码验证
</code></pre><ol start="8">
<li><p>关于session </p>
<pre><code>Shiro默认会使用Servlet容器的Session,可通过sessionMode属性来指定使用Shiro原生Session 

即&lt;property name=&quot;sessionMode&quot; value=&quot;native&quot;/&gt;,详细说明见官方文档
</code></pre></li>
</ol>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/02/01/CAP原理和BASE思想/" rel="prev" title="CAP原理和BASE思想">
                CAP原理和BASE思想 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">S-Name Small·kk</p>
              <p class="site-description motion-element" itemprop="description">开源 | Java | 个人笔记 | 我的博客</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">12</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Shiro步骤或注意事项："><span class="nav-number">1.</span> <span class="nav-text">Shiro步骤或注意事项：</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#7"><span class="nav-number">1.0.1.</span> <span class="nav-text">==7==.</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">S-Name Small·kk</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
